//
//  PYRNBridge.m
//  IHome
//
//  Created by 朱吉峥 on 2020/2/1.
//  Copyright © 2020 com.zhujizheng. All rights reserved.
//

#import "PYRNBridge.h"
#import <React/RCTBundleURLProvider.h>
#import "PYRNUpdate.h"

// dev模式下：RCTBridge required dispatch_sync to load RCTDevLoadingView Error Fix
#if RCT_DEV
#import <React/RCTDevLoadingView.h>
#endif
/// 自定义类，实现RCTBridgeDelegate
@interface PYRNBridgeHandle : NSObject <RCTBridgeDelegate>

@end

@implementation PYRNBridgeHandle

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
    return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
    return [PYRNUpdate bundleURL];
#endif
}

@end

@implementation PYRNBridge

static PYRNBridge *bridge = nil;

+ (instancetype)sharedBridge
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bridge = [[PYRNBridge alloc] initWithDelegate:[PYRNBridgeHandle new] launchOptions:nil];
#if RCT_DEV
        [bridge moduleForClass:[RCTDevLoadingView class]];
#endif
    });
    return bridge;
}

+ (void)reload
{
    bridge = [[PYRNBridge alloc] initWithDelegate:[PYRNBridgeHandle new] launchOptions:nil];
#if RCT_DEV
    [bridge moduleForClass:[RCTDevLoadingView class]];
#endif
}

@end
